# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\excluded_hours.py
import datetime
from dateutil.tz import tzoffset
from sqlalchemy import select as sql_select
from db.tables.system_config import SystemConfigRow
from db.tables.excluded_hours import ExcludedHoursRow
from db.tables.system_config_mu import SystemConfigMuRow

_weekday_names = ("sun", "mon", "tue", "wed", "thu", "fri", "sat")


def get_system_excluded_hours(connection):
    q = sql_select((SystemConfigRow.value,)).where(
        SystemConfigRow.name == "excluded_hours_id"
    )
    excluded_hours_id = connection.execute(q).scalar()
    if not excluded_hours_id:
        return
    q = sql_select(
        (ExcludedHoursRow.name, ExcludedHoursRow.time_offset, ExcludedHoursRow.data)
    ).where(ExcludedHoursRow.excluded_hours_id == excluded_hours_id)
    r = connection.execute(q).fetchone()
    if not r:
        return
    else:
        return dict(exclusion_data=r.data, time_offset=r.time_offset, name=r.name)


def get_per_account_excluded_hours(connection, user_id):
    q = (
        sql_select((SystemConfigMuRow.value,))
        .where(SystemConfigMuRow.user_id == user_id)
        .where(SystemConfigMuRow.name == "excluded_hours_id")
    )
    excluded_hours_id = connection.execute(q).scalar()
    if not excluded_hours_id:
        return
    q = sql_select(
        (ExcludedHoursRow.name, ExcludedHoursRow.time_offset, ExcludedHoursRow.data)
    ).where(ExcludedHoursRow.excluded_hours_id == excluded_hours_id)
    r = connection.execute(q).fetchone()
    if not r:
        return
    else:
        return dict(exclusion_data=r.data, time_offset=r.time_offset, name=r.name)


def is_allowed_to_scan_now(exclusion_data, time_offset=0, **_):
    if not exclusion_data or time_offset is None:
        return True
    else:
        exclusion_matrix = exclusion_data.get("exclusion_matrix")
        if not exclusion_matrix:
            return True
        now = datetime.datetime.now(tz=tzoffset(None, time_offset * 60))
        weekday = now.strftime("%A").lower()[0:3]
        current_hour = now.hour
        weekday_index = _weekday_names.index(weekday)
        return not exclusion_matrix[weekday_index * 24 + current_hour]


def get_next_date_allowed_to_scan(exclusion_data, time_offset=0, **_):
    now = datetime.datetime.now(tz=tzoffset(None, time_offset * 60))
    exclusion_matrix = exclusion_data.get("exclusion_matrix")
    if not exclusion_matrix:
        return now
    else:
        weekday = now.strftime("%A").lower()[0:3]
        current_hour = now.hour
        weekday_index = _weekday_names.index(weekday)
        em_ix = weekday_index * 24 + current_hour
        if not exclusion_matrix[em_ix]:
            return now
        for i in range(0, len(exclusion_matrix)):
            if not exclusion_matrix[(i + em_ix) % 168]:
                return now + (datetime.timedelta(hours=i))

        return False
